<?php

class Application_Model_RemoteFee extends Application_Model_DomainObject{
    
    protected $_id;
    protected $_stateId;
    protected $_startZip;
    protected $_endZip;
    
    const remoteFee = 100;


    public static function byStateIdZip($stateId,$zip)
    {
        $where[" stateId = "] = $stateId;
        $where[" and startZip <= "] = $zip;
        $where[" and endZip >= "] = $zip;
        $remoteFeeItem = self::getDao()->queryAll($where);
        if(count($remoteFeeItem)>0){
            return true;
        } else {
            return false;
        }
    }
    
    public static function getRemoteFeeFileds($startId,$startZip,$endZip,$pagesize,$pageindex)
    {
        $where = array();
        $and = "";
        if($startId != "")
        {
            $where[$and." stateId = "] = $startId;
            $and = " and ";
        }
        if($startZip != "")
        {
            $where[$and." startZip <= "] = $startZip;
            $and = " and ";
        }
        if($endZip != "")
        {
            $where[$and." endZip >= "] = $endZip;
            $and = " and ";
        }
        return self::getDao()->queryPage($where, "id desc ", $pagesize, $pageindex);
    }
    
    private $_startName;
    public function getStateName()
    {
        if ($this->_startName == NULL)
            $this->_startName = Application_Model_city::load($this->_stateId)->Name;
        return $this->_startName;
    }
    
    public static function addOrEditRemoteFee($editId,$stateId,$startZip,$endZip)
    {
        $pd = true;
        if($editId) {
            $remoteFeeItem = self::load($editId);
            if(!$remoteFeeItem){
                $remoteFeeItem = new Application_Model_RemoteFee();
                $pd = false;
            }
        } else {
            $remoteFeeItem = new Application_Model_RemoteFee();
            $pd = false;
        }
        $remoteFeeItem->StateId = $stateId;
        $remoteFeeItem->StartZip = $startZip;
        $remoteFeeItem->EndZip = $endZip;
        $remoteFeeItem->save();
        Application_Model_Operate::addOperate(0, null, $remoteFeeItem->Id, $pd?"修改偏远费":"添加偏远费", 0);
        return $remoteFeeItem->Id;
    }
    
    public static function EastOrWestern($zip)
    {
        $ifZip = substr($zip,0,2);
        if($ifZip >= 87 && $ifZip <= 99){
            return true;
        } else {
            return false;
        }
    }
}
?>
